﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LoadClient.YUAN.C
{
    public class COP
    {

        public static string GetGuid()
        {
            return Guid.NewGuid().ToString().Replace("-", "").ToUpper();
        }


        public static string GetAppSettingsValue(string keyname)
        {
            try
            {
                return ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
              .AppSettings.Settings[keyname].Value;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;

            }
        }

        public static void SetAppSettingsValue(string keyname, string value)
        {
            try
            {
                Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.AppSettings.Settings[keyname].Value = value;
                config.Save();
            }
            catch (Exception ex)
            {

                Console.WriteLine(ex.ToString());
            }

        }

  

        public static string FormatCommand(object value, string kodname, int index, string guid)
        {
            string operationmode = "KI";//通过地址关键字+index写入
            if (index.Equals(1000)) operationmode = "DD";//通过地址写入
            if (index.Equals(2000)) operationmode = "NT";//通过地址关键字取取反
            if (index.Equals(3000)) operationmode = "NG";//校验写入的起点和终点                  
            return string.Format(@"{0}#{1}#{2}#{3}#{4}", operationmode, value, kodname, index, guid);
        }

        public static void WriteToDataTable(DataTable dataTable, object args)
        {
            ArrayList list = (args as ArrayList);
            dataTable.BeginLoadData();
            for (int i = 0; i < list.Count; i++)
            {
                DataRow dataRow = dataTable.NewRow();
                var dlist = (list[i] as Array);
                for (int j = 0; j < dlist.Length; j++)
                {
                    dataRow[j] = dlist.GetValue(j);
                }
                dataTable.Rows.Add(dataRow);
            }
            dataTable.EndLoadData();
        }

       

       

    }
}
